package proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * User:xuzhidian
 * Date:2018/4/2
 * Time:23:07
 */
public class CglibScalper implements MethodInterceptor {

    public Object getInstace(Class clazz) {
        Enhancer enhancer = new Enhancer();
        //要把哪个类设置为即将生成的新类的父类
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("排队");
        methodProxy.invokeSuper(o, objects);
        System.out.println("ok!");
        return null;
    }
}
